package keyfun.parsers 
{
	/**
	 * XML Parser Class
	 * @author Key Hui
	 */
	public class XMLParser
	{
		
		/**
		 * Constructor
		 */
		public function XMLParser() 
		{
			
		}
		
		public static function removeNamespaces(xml:XML):XML
		{
			var xmlString:String;
			var responseXML:XML;
			xmlString = xml.toXMLString();
			xmlString = xmlString.replace( new RegExp("xmlns(:[A-Za-z0-9]+)?=[^\"]*\"[^\"]*\"", "gi"), "" );
			xmlString = xmlString.replace( new RegExp( "<*:", "gi" ), "_");
			responseXML = new XML(xmlString);
			return responseXML;
		}
		
		public static function getXMLArray(xmlList:XMLList):Array
		{
			var ele:XML;
			var dataArr:Array = new Array();
			//trace(xmlList);
			for each(ele in xmlList)
			{
				var dataObj:Object = new Object();
				//trace(ele.name() + ": " + String(ele));
				dataObj[String(ele.name())] = String(ele);
				dataArr.push(dataObj);
			}
			return dataArr;
		}
		
		public static function getXMLObjectArray(xmlList:XMLList):Array
		{
			var dataXML:XML;
			var ele:XML;
			var dataArr:Array = new Array();
			//trace(xmlList);
			for each(dataXML in xmlList)
			{
				var dataObj:Object = new Object();
				for each (ele in dataXML.children())
				{
					//trace(ele.name() + ": " + String(ele));
					dataObj[String(ele.name())] = String(ele);
				}
				dataArr.push(dataObj);
			}
			return dataArr;
		}
		
		public static function getXMLObject(xmlList:XMLList):Object
		{
			var ele:XML;
			var dataObj:Object = new Object();
			
			for each (ele in xmlList)
			{
				//trace(ele.name() + ": " + String(ele));
				dataObj[String(ele.name())] = String(ele);
			}
			return dataObj;
		}
	}

}